<?php

namespace app\models;

use Yii;

class UserModel
{
    public function getAllUser(){
        return \Yii::$app->db->createCommand("select * from p_user")->queryAll();
    }

    public function getOneUser($user_id){
        return \Yii::$app->db->createCommand("select * from p_user where user_id=:user_id",[":user_id"=>$user_id])->queryOne();
    }

    public function findIdentity($user_id){
        $userIdentity="user_id,username,password,email,authKey,accessToken";
        return \Yii::$app->db->createCommand("select $userIdentity from p_user where user_id=:user_id",[":user_id"=>$user_id])->queryOne();
    }

    public function findByUsername($username){
        $userIdentity="user_id,username,password,email,authKey,accessToken";
        return \Yii::$app->db->createCommand("select $userIdentity from p_user where username=:username",[":username"=>$username])->queryOne();
    }

    public function findByEmail($email){
        $userIdentity="user_id,username,password,email,authKey,accessToken";
        return \Yii::$app->db->createCommand("select $userIdentity from p_user where email=:email",[":email"=>$email])->queryOne();
    }

    public function findIdentityByAccessToken($accessToken){
        $userIdentity="user_id,username,password,email,authKey,accessToken";
        return \Yii::$app->db->createCommand("select $userIdentity from p_user where accessToken=:accessToken",[":accessToken"=>$accessToken])->queryOne();
    }

    public function insertUser($data){
        \Yii::$app->db->createCommand()->insert("p_user",$data)->execute();
        return \Yii::$app->db->getLastInsertId();
    }

    public function updateUser($data,$user_id){
        return \Yii::$app->db->createCommand()->update("p_user",$data,["user_id"=>$user_id])->execute();
    }

    public function deleteUser($user_id){
        return \Yii::$app->db->createCommand()->delete("p_user",["user_id"=>$user_id])->execute();
    }
}